package com.auxgroup.smarthome.inner.service;

import com.auxgroup.bridge.business.inner.vo.RegionVo;
import com.auxgroup.bridge.business.inner.vo.ThirdCascaderVo;
import com.auxgroup.smarthome.BeanListUtils;
import com.auxgroup.smarthome.inner.entity.RegionDatabaseEntity;
import com.auxgroup.smarthome.inner.repo.RegionDatabaseRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 行政区划分服务
 * Created by Toby Lai on 2017/7/3.
 */
@Service
public class AdministrativeDivisionService {
    @Autowired
    private RegionDatabaseRepo regionDatabaseRepo;

    public List<RegionVo> findAll() {
        RegionDatabaseEntity regionDatabaseEntity = new RegionDatabaseEntity();
        regionDatabaseEntity.setLevelType(1);
        Example example = Example.of(regionDatabaseEntity);
        return new BeanListUtils(RegionVo.class,regionDatabaseRepo.findAll(example)).getTargetList();
    }

    public List<RegionVo> findByProvinceId(String provinceId) {
        RegionDatabaseEntity regionDatabaseEntity = new RegionDatabaseEntity();
        regionDatabaseEntity.setLevelType(2);
        regionDatabaseEntity.setParentId(provinceId);
        Example example = Example.of(regionDatabaseEntity);
        return new BeanListUtils(RegionVo.class,regionDatabaseRepo.findAll(example)).getTargetList();
    }

    public List<RegionVo> findByCityId(String cityId) {
        RegionDatabaseEntity regionDatabaseEntity = new RegionDatabaseEntity();
        regionDatabaseEntity.setLevelType(3);
        regionDatabaseEntity.setParentId(cityId);
        Example example = Example.of(regionDatabaseEntity);
        return new BeanListUtils(RegionVo.class,regionDatabaseRepo.findAll(example)).getTargetList();
    }

    public List<ThirdCascaderVo> getThirdCasader() {
        List<RegionDatabaseEntity> provinces = regionDatabaseRepo.findByLevelType(1); // 获取省份
        List<RegionDatabaseEntity> citys = regionDatabaseRepo.findByLevelType(2); // 获取城市
        List<RegionDatabaseEntity> distributs = regionDatabaseRepo.findByLevelType(3); // 获取区
        List<ThirdCascaderVo> provineVoList = new ArrayList<>();
        provinces.stream().forEach(province -> {
            String provinceId = province.getId();
            String provinceName = province.getName();
            String provinceCode = province.getCityCode();
            String provinceShortName = province.getShortName();
            List<ThirdCascaderVo> cityList = new ArrayList<>();
            citys.stream().forEach(city -> {
                String cityId = city.getId();
                String cityCode = city.getCityCode();
                String cityShortName = city.getShortName();
                String cityParentId = city.getParentId();
                String cityName = city.getName();
                if (cityParentId.equals(provinceId)) {
                    List<ThirdCascaderVo> distriList = new ArrayList<>();
                    distributs.stream().forEach(distribut -> {
                        String distributId = distribut.getId();
                        String distributCode = distribut.getCityCode();
                        String distributShortName = distribut.getShortName();
                        String distributParentId = distribut.getParentId();
                        String distributName = distribut.getName();
                        if (distributParentId.equals(cityId)) {
                            ThirdCascaderVo distriVo = new ThirdCascaderVo(distributName, null).fill(distributId, distributCode, distributShortName);
                            distriList.add(distriVo);
                        }
                    });
                    ThirdCascaderVo cityVo = new ThirdCascaderVo(cityName, distriList).fill(cityId, cityCode, cityShortName);
                    cityList.add(cityVo);
                }
            });
            ThirdCascaderVo cityVo = new ThirdCascaderVo(provinceName, cityList).fill(provinceId, provinceCode, provinceShortName);
            provineVoList.add(cityVo);
        });
        return provineVoList;
    }

}
